﻿using MediaToolkit;
using MediaToolkit.Model;
using MediaToolkit.Options;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VideoSpliter.Util;

namespace VideoSpliter.Model
{
    public class MetaData:ObservableObject
    {
        private string _path;
        private VideoData _videoData;
        private AudioData _audioData;
        private ObservableCollection<string> _thumbnails;
        private string _name;

        public string Path
        {
            get { return _path; }
            set { SetProperty(ref _path,value); }
        }

        public string Name
        {
            get { return _name; }
            set { SetProperty(ref _name,value); }
        }

        public VideoData VideoData
        {
            get { return _videoData; }
            set { SetProperty(ref _videoData,value); }
        }

        public AudioData AudioData
        {
            get { return _audioData; }
            set { SetProperty(ref _audioData,value);}
        }

        public ObservableCollection<string> Thumbnails
        {
            get { return _thumbnails; }
            set { SetProperty(ref _thumbnails,value);}
        }

        public static MetaData Read(string path)
        {
            using (var engine = new Engine())
            {
                var inputFile = new MediaFile(path);
              
                engine.GetMetadata(inputFile);
                var metadata = inputFile.Metadata;
                var trumbnails = ThumbnailManager.AddKey(path);
                if (trumbnails.Count==0)
                {
                    for (int i = 1; i <= 4; i++)
                    {
                        var outputFile = new MediaFile(ThumbnailManager.GenerateFileName(path));
                        engine.GetThumbnail(
                            inputFile, outputFile,
                            new ConversionOptions() { Seek = TimeSpan.FromSeconds(15*i) }
                        );
                        trumbnails.Add(outputFile.Filename);
                    }
                   

                }
                return new MetaData
                {
                    Path = path,
                    Name = System.IO.Path.GetFileNameWithoutExtension(path),
                    VideoData = new VideoData()
                    {
                        VideoBitrate = metadata.VideoData.BitRateKbs,
                        Resolution = metadata.VideoData.FrameSize,
                        FPS = metadata.VideoData.Fps,
                        VideoFormat = metadata.VideoData.Format,
                        Duration = metadata.Duration
                        
                    },
                    AudioData = new AudioData()
                    {
                        AudioBitrate = metadata.AudioData.BitRateKbs,
                        SampleRate = metadata.AudioData.SampleRate,
                        AudioFormat = metadata.AudioData.Format
                    },
                    Thumbnails = ThumbnailManager.GetThumbnails(path)
                };

            }
        }

    }

    public class VideoData:ObservableObject
    {
        private double? _videoBitrate;
        private string _resolution;
        private TimeSpan _duration;
        private double _fps;
        private string _videoFormat;

        public double? VideoBitrate
        {
            get { return _videoBitrate; }
            set {  SetProperty(ref _videoBitrate,value);}
        }

        public string Resolution
        {
            get { return _resolution; }
            set { SetProperty(ref _resolution,value);}
        }

        public TimeSpan Duration
        {
            get { return _duration; }
            set { SetProperty(ref _duration,value); }
        }

        public double FPS
        {
            get { return _fps; }
            set { SetProperty(ref _fps,value);}
        }

        public string VideoFormat
        {
            get { return _videoFormat; }
            set { SetProperty(ref _videoFormat,value); }
        }
    }

    public class AudioData:ObservableObject
    {
        private double? _audioBitrate;
        private string _audioFormat;
        private string _sampleRate;

        public double? AudioBitrate
        {
            get { return _audioBitrate; }
            set { SetProperty(ref _audioBitrate,value); }
        }

        public string AudioFormat
        {
            get { return _audioFormat; }
            set { SetProperty(ref _audioFormat, value); }
        }

        public string SampleRate
        {
            get { return _sampleRate; }
            set { SetProperty(ref _sampleRate,value); }
        }
    }
}
